﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace PhotonServer
{
    class Program
    {
        static void Main(string[] args)
        {
            // Update();
            Read();


        }

        static void Read() 
        {
            string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=1234;";
            MySqlConnection conn = new MySqlConnection(connectStr);
            try
            {
                conn.Open();
                string sql = "select * from users";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                //cmd.ExecuteReader();//执行查询
                //cmd.ExecuteNonQuery();//插入 删除
                //cmd.ExecuteScalar();//执行一些查询，返回一个单个值
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    // Console.WriteLine(reader[0].ToString() + reader[1].ToString() + reader[2].ToString());
                    Console.WriteLine(reader.GetInt32("id")+"----"+ reader.GetString("username")+"---"+reader.GetString("password"));
                }

            
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }

      
        static void Insert() 
        {
            string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=1234;";
            MySqlConnection conn = new MySqlConnection(connectStr);
            try
            {
                conn.Open();
                //    string sql = "insert into users(username,password) values('cxlvkee','243')";
                //  string sql = "insert into users(username,password,registerdate) values('cxlvkees','243','2014-01-09')";
                string sql = "insert into users(username,password,registerdate) values('cxlvkees','243','"+DateTime.Now+"')";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int reader = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }
        static void Update()
        {
            string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=1234;";
            MySqlConnection conn = new MySqlConnection(connectStr);
            try
            {
                conn.Open();
                //    string sql = "insert into users(username,password) values('cxlvkee','243')";
                string sql = "UPDATE `mygamedb`.`users` SET `username`='sdfw324ee', `password`='34ee' WHERE `id`='5'";
              //  string sql = "update users set  username='sdsdsd',password='121123123' where id =4";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int reader = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }
        static void Delete()
        {
            string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=1234;";
            MySqlConnection conn = new MySqlConnection(connectStr);
            try
            {
                conn.Open();
                //    string sql = "insert into users(username,password) values('cxlvkee','243')";
                string sql = "delete from users where id =4";
                //  string sql = "update users set  username='sdsdsd',password='121123123' where id =4";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int reader = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }
    }
}
